// Copyright (c) 2011 Alexander Gnodtke. All rights reserved.

// This file is part of MAD (Multi Address Display) browser extension.

// MAD (Multi Address Display) is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// MAD (Multi Address Display) is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with MAD (Multi Address Display).  If not, see <http://www.gnu.org/licenses/>.

// Array of TODO
var addresses = [];

function parseText(text) {	
	text = text.trim();	
	text = text.replace(/(\r\n|\n|\r)/gm,", ");		
	return text
}

function geoCodeAddress(address) {
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': address}, 
			function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					geocode = results[0].geometry.location;					
					addresses[addresses.length] = [address,geocode.lat(),geocode.lng()]; 				
				} else {
					console.log("Geocode was not successful for the following reason: " + status);
					alert(chrome.i18n.getMessage("errAddrNotValid")+status);
				}
			});
}

// Add Address to list of addresses to be displayed
function addAddress(info, tab) {	
	if (info.selectionText) {		
		// parse text for address
		var address = parseText(info.selectionText);
		// geocode, validate and add address
		geoCodeAddress(address);							
  }
}

function clearAddresses(info, tab) {
	addresses = [];
}

function displayMap(info, tab) {
	if (addresses.length > 0) {
		chrome.tabs.create({"url": "map.html","selected":true});
	} else {
		alert(chrome.i18n.getMessage("msgSelectAddr"));
	}
}

function sendAddress(callback) {
	console.log("sendAddress "+addresses);
	callback(addresses);
}

function onRequest(request, sender, callback) {        
        if (request.action == 'getAddresses') {
          console.log("getAddresses");
          sendAddress(callback);
        }
      };
      
// Listen for address request from map.html
chrome.extension.onRequest.addListener(onRequest);


// Create menu Item for select
var masterMenu = chrome.contextMenus.create({"title": chrome.i18n.getMessage("menuExtTitle"),
			"contexts":["all"],}); 
chrome.contextMenus.create({"title": chrome.i18n.getMessage("menuAddAddr"),
			"contexts":["selection"],
                        "onclick": addAddress,
			"parentId":masterMenu});
chrome.contextMenus.create({"title": chrome.i18n.getMessage("menuDisplayMap"),
			"contexts":["all"],
                        "onclick": displayMap,
			"parentId":masterMenu});
chrome.contextMenus.create({"title": chrome.i18n.getMessage("menuClearAddr"),
			"contexts":["all"],
                        "onclick": clearAddresses,
			"parentId":masterMenu});

  

